*** Generating figure 3: main results split by subsamples ***

** Preamble **

clear all
set more off


local path_input  "C:/Users/$user/Dropbox/$project/master/output/data"
local path_output "C:/Users/$user/Dropbox/$project/master/output/figures"

* Note: local directory is the output folder 
cd "`path_output'"

				** Loading the input file **

use `path_input'/dataset_final_may20, clear

estimates clear

xtset codigo_ibge turno

local baseline_controls "pop1991 years_sch1991 renda1991 sh_tv1991 sh_agua1991 sh_elect1991 sh_rural"
local CGB_FEs			"pct_sh_tv1991 pct_sh_rural pct_renda1991"


/*generating percentiles*/
foreach var in `baseline_controls' {
xtile pct_`var'=  `var', n(10)
replace pct_`var'=0 if turno==2
}

/*generating pscores*/
encode uf, gen(UF)
psmatch2 Globo T1* if turno==1,  noreplace neighbor(1)
egen ppp=mean(_pscore), by(codigo)
gen pscore=1
replace pscore=ppp/(1-ppp) if Globo==0

label var treatment "Globo"
label var treatment1 "Globo and Others"
label var treatment2 "Globo Only"


									*** Graphs A,B: votes for Left-Right ***

											** I **
											
** spec 1: No controls **
							
eststo: xi: reghdfe sh_Tesquerda treatment1 treatment2 			   turno,             vce(cluster codigo_ibge) a(codigo_ibge)
					
** spec 2: Group FE **
					
eststo: xi: reghdfe sh_Tesquerda treatment1 treatment2 			   turno, 			  vce(cluster codigo_ibge) a(codigo_ibge `CGB_FEs')

** spec 3: Reweighting **
					
eststo: xi: reghdfe sh_Tesquerda treatment1 treatment2 			   turno [pw=pscore], vce(cluster codigo_ibge) a(codigo_ibge)


											** II **
																				
** spec 1: No controls **
							
eststo: xi: reghdfe sh_Tdireita treatment1 treatment2 			   turno,             vce(cluster codigo_ibge) a(codigo_ibge)
					
** spec 2: Group FE **
					
eststo: xi: reghdfe sh_Tdireita treatment1 treatment2 			   turno, 			  vce(cluster codigo_ibge) a(codigo_ibge `CGB_FEs')

** spec 3: Reweighting **
					
eststo: xi: reghdfe sh_Tdireita treatment1 treatment2 			   turno [pw=pscore], vce(cluster codigo_ibge) a(codigo_ibge)
						

							** Graphs C,D: votes for Lula-Collor **

											** I **
											
** spec 1: No controls **
							
eststo: xi: reghdfe sh_Tpt_ treatment1 treatment2 sh_Tpsp_-sh_Tpsdb_ turno, 			vce(cluster codigo_ibge) a(codigo_ibge)
					
** spec 2: Group FE **
					
eststo: xi: reghdfe sh_Tpt_ treatment1 treatment2 sh_Tpsp_-sh_Tpsdb_ turno, 			vce(cluster codigo_ibge) a(codigo_ibge `CGB_FEs')

** spec 3: Reweighting **
					
eststo: xi: reghdfe sh_Tpt_ treatment1 treatment2 sh_Tpsp_-sh_Tpsdb_ turno [pw=pscore], vce(cluster codigo_ibge) a(codigo_ibge)
											
																
											** II **
** spec 1: No controls **
							
eststo: xi: reghdfe sh_Tprn_ treatment1 treatment2 sh_Tpsp_-sh_Tpsdb_ turno, 			vce(cluster codigo_ibge) a(codigo_ibge)
					
** spec 2: Group FE **
					
eststo: xi: reghdfe sh_Tprn_ treatment1 treatment2 sh_Tpsp_-sh_Tpsdb_ turno, 			vce(cluster codigo_ibge) a(codigo_ibge `CGB_FEs')

** spec 3: Reweighting **
					
eststo: xi: reghdfe sh_Tprn_ treatment1 treatment2 sh_Tpsp_-sh_Tpsdb_ turno [pw=pscore], vce(cluster codigo_ibge) a(codigo_ibge)

						
********************************************************************************


										*** Main results: heterogeneous effects ***


coefplot (est1, msymbol(D) color(black) offset(0.00)   ciopts(recast(rcap) lcol(black)) text( .010 1.25 "Votes for left-wing candidates", size(small))  )  ///                   
		 (est2, msymbol(S) color( navy) offset(0.25)   ciopts(recast(rcap) lcol( navy)) 				  											    )  ///
		 (est3, msymbol(C) color(  red) offset(0.50)   ciopts(recast(rcap) lcol(  red)) 												 			    )  ///
		 (est4, msymbol(D) color(black) offset(1.50)   ciopts(recast(rcap) lcol(black)) text(-.010 2.65 "Votes for right-wing candidates", size(small)) )  ///                   
		 (est5, msymbol(S) color( navy) offset(1.75)   ciopts(recast(rcap) lcol( navy))											     			        )  ///     
		 (est6, msymbol(C) color(  red) offset(2.00)   ciopts(recast(rcap) lcol(  red)) 				 								 			    ), ///
		 ///
		 vertical  						 				          			       				///
		 keep(treatment2)                 				  		  			       				/// 
		 yline(0, lpattern(dash) lcolor(grey))          		  			       				///
  		 ylabel(      -0.05(0.02)0.05) 											   				///
		 yscale(range(-0.05(0.02)0.05))											   				///
 		 graphregion(color(white))                                			       				///
		 xlab("")                                       		  			       				///
		 plotlabels("Unconditional" "Group FEs" "Reweighted" " " " " " " " " " " " ")   		///
		 saving("Figure_baseline_Vote_Shares_Subsamples_graph_A.png", replace) 					///
		 legend(rows(1))
		 
		 graph export Figure_baseline_Vote_Shares_Subsamples_graph_A.png, replace
		 

 
 
coefplot (est7,  msymbol(D) color(black) offset(0.00)  ciopts(recast(rcap) lcol(black)) text( .005 1.25  "Lula's vote share",   			  size(small)) )  ///                   
		 (est8,  msymbol(S) color( navy) offset(0.25)  ciopts(recast(rcap) lcol( navy)) 				  						         				   )  ///
		 (est9,  msymbol(C) color(  red) offset(0.50)  ciopts(recast(rcap) lcol(  red)) 												 				   )  ///
		 (est10, msymbol(D) color(black) offset(1.50)  ciopts(recast(rcap) lcol(black)) text(-.005 2.75  "Collor's vote share", 			  size(small)) )  ///
		 (est11, msymbol(S) color( navy) offset(1.75)  ciopts(recast(rcap) lcol( navy))    											 				   	   )  ///
		 (est12, msymbol(C) color(  red) offset(2.00)  ciopts(recast(rcap) lcol(  red))                                                 				   ), ///
		 ///
		 vertical  						 				          			       				///
		 keep(treatment2)                 				  		  			       				/// 
		 yline(0, lpattern(dash) lcolor(grey))          		  			       				///								 
   		 ylabel(      -0.05(0.02)0.05) 											   				///
		 yscale(range(-0.05(0.02)0.05))											   				///
	 	 graphregion(color(white))                                			       				///
		 xlab("")                                       		  			       				///
		 plotlabels("Controls" "Group FEs""Reweighted" " " " " " " " " " " " ")    				///
		 saving("Figure_baseline_Vote_Shares_Subsamples_graph_B.png", replace) 					///
		 legend(rows(1))

		 graph export Figure_baseline_Vote_Shares_Subsamples_graph_B.png, replace
		 
		 
coefplot (est1, msymbol(D) color(black) offset(0.00)   ciopts(recast(rcap) lcol(black)) text( .010 1.25 "Votes for left-wing candidates", size(small))  )  ///                   
		 (est2, msymbol(S) color( navy) offset(0.25)   ciopts(recast(rcap) lcol( navy)) 				  											    )  ///
		 (est3, msymbol(C) color(  red) offset(0.50)   ciopts(recast(rcap) lcol(  red)) 												 			    )  ///
		 (est4, msymbol(D) color(black) offset(1.50)   ciopts(recast(rcap) lcol(black)) text(-.028 2.65 "Votes for right-wing candidates", size(small)) )  ///                   
		 (est5, msymbol(S) color( navy) offset(1.75)   ciopts(recast(rcap) lcol( navy))											     			        )  ///     
		 (est6, msymbol(C) color(  red) offset(2.00)   ciopts(recast(rcap) lcol(  red)) 				 								 			    ), ///
		 ///
		 vertical  						 				          			       				///
		 keep(treatment1)                 				  		  			       				/// 
		 yline(0, lpattern(dash) lcolor(grey))          		  			       				///
		 ylabel(      -0.05(0.02)0.05) 											   				///
		 yscale(range(-0.05(0.02)0.05))											   				///
		 graphregion(color(white))                                			       				///
		 xlab("")                                       		  			       				///
		 plotlabels("Unconditional" "Group FEs" "Reweighted" " " " " " " " " " " " ")   		///
		 saving("Figure_baseline_Vote_Shares_Subsamples_graph_C.png", replace) 					///
		 legend(rows(1))

		 graph export Figure_baseline_Vote_Shares_Subsamples_graph_C.png, replace


		 
coefplot (est7,  msymbol(D) color(black) offset(0.00)  ciopts(recast(rcap) lcol(black)) text( .005 1.25  "Lula's vote share",   			  size(small)) )  ///                   
		 (est8,  msymbol(S) color( navy) offset(0.25)  ciopts(recast(rcap) lcol( navy)) 				  						         				   )  ///
		 (est9,  msymbol(C) color(  red) offset(0.50)  ciopts(recast(rcap) lcol(  red)) 												 				   )  ///
		 (est10, msymbol(D) color(black) offset(1.50)  ciopts(recast(rcap) lcol(black)) text(-.010 2.75  "Collor's vote share", 			  size(small)) )  ///
		 (est11, msymbol(S) color( navy) offset(1.75)  ciopts(recast(rcap) lcol( navy))    											 				   	   )  ///
		 (est12, msymbol(C) color(  red) offset(2.00)  ciopts(recast(rcap) lcol(  red))                                                 				   ), ///
		 ///
		 vertical  						 								          			    ///
		 keep(treatment1)                 				  		  			       				/// 
		 yline(0, lpattern(dash) lcolor(grey))           						   				///								 
		 ylabel(      -0.05(0.02)0.05) 											   				///
		 yscale(range(-0.05(0.02)0.05))											   				///
 		 graphregion(color(white))                                			       				///
		 xlab("")                                       		  			       				///
		 plotlabels("Controls" "Group FEs""Reweighted" " " " " " " " " " " " ")    				///
		 saving("Figure_baseline_Vote_Shares_Subsamples_graph_D.png", replace) 					///
		 legend(rows(1)) 
		 
		 graph export Figure_baseline_Vote_Shares_Subsamples_graph_D.png, replace
		 		 
										